package no.weather.weatherProvider;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import no.weather.widget.WidgetProvider;

public class WeatherReportBroadcaster extends Service {
	/*
	 * private WeatherStation mWeatherStation; private Double
	 * minUpdateFrequenceSeconds; private Set<WeatherType> neededWeatherTypes;
	 * 
	 * public WeatherReportBroadcaster(WeatherStation mWeatherStation, Double
	 * minUpdateFrequenceSeconds, Set<WeatherType> neededWeatherTypes) {
	 * super(); this.mWeatherStation = mWeatherStation;
	 * this.minUpdateFrequenceSeconds = minUpdateFrequenceSeconds;
	 * this.neededWeatherTypes = neededWeatherTypes; }
	 */

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	// This is the old onStart method that will be called on the pre-2.0
	// platform. On 2.0 or later we override onStartCommand() so this
	// method will not be called.
	@Override
	public void onStart(Intent intent, int startId) {
		start();
	}

	private void start() {
		AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		int minutes = time.get(Calendar.MINUTE);
		time.add(Calendar.MINUTE, 60 - minutes);
		Intent intent = new Intent();
		intent.setAction(WidgetProvider.UPDATE_WEATHER_ACTION);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
				PendingIntent.FLAG_ONE_SHOT);
		alarm.set(AlarmManager.RTC, time.getTimeInMillis(), pendingIntent);
	}

}
